/*单位管理*/
(function($){
	var Unit = {
		runtime : {
			'type' : null,
			'oper' : null 
		},
				
		options : {
			'links':'.unit_admin',
			'dialogId':'#unit',
			'attr-type':'data-type',
			'attr-op':'data-op',
			'attr-id':'data-id',
			'attr-super-id':'data-super-id'	
		},	
		
		bind : function(){
			var self = this,
				links = self.options['links'];
			
			self.register(links);
		},
		
		register : function(selector){
			var self = this, 
				me = null,
				attrType = self.options['attr-type'],
				attrOp = self.options['attr-op'];
				
			$(selector).bind('click',function(){
				me = $(this);
				self.runtime.type = me.attr(attrType);
				self.runtime.oper = me.attr(attrOp);
				
				self.op(self.runtime.type,self.runtime.oper,this);
			});
		},
		
		//table:操作表(省表\市表\区县表)
		//oper:操作名(增\删\改)
		//target:操作记录		
		op : function(table, oper, target){
			var self = this,
				dialog = null; 		
			
			self.resets(table, oper, target);			
		},
		
		//table:操作表(省表\市表\区县表\站点)
		//oper:操作名(增\删\改)
		//target:操作记录
		//dialog:操作层		
		resets : function(table, oper, target){
			var self = this, 
				record = $(target),
				option = self.options,
				dialogLayer = $(self.options.dialogId),
				id = record.attr(option['attr-id']),
				super_id = '',
				super_table = '', 
				super_name = '',
				name = '',
				title = '',
				css = '',
				only = '';
				
			//if(oper == 'add'){	
				switch(table){
					case 'CITY':
						unit_type_name = '市级';
						super_table = 'PROVINCE';
						break;
					case 'COUNTY':
						unit_type_name = '区县级';
						super_table = 'CITY';
						break;
					case 'BASE':
						unit_type_name = '站点级';
						super_table = 'COUNTY';
				}
			//}
			
			if(oper == 'add'){	
				super_name = (table == 'PROVINCE')?"无":$("#"+super_table+"_"+id).text();
				name = '';
			}else{
				super_id = $(target).parent().parent().attr(option['attr-super-id']);
				if(super_id){
					super_name = $.trim($("#"+super_id).text());
				}
				name = $.trim($("#"+table+"_"+id).text());							
			}			
					
			
			
			if(oper == 'delete'){
				css = "";
				only = true;
				title = "删除单位";
			}else{
				css = "";
				only = false;
				title = (oper == 'add')?"新增单位":"编辑单位";				
			}
			
			dialogLayer.find("input[name=unit_name]").val(name);
			//dialogLayer.find("input[name=unit_name]").removeAttr('readonly').val(name);
			dialogLayer.find("input[name=unit_type]").val(table).attr('readonly','readonly');
			dialogLayer.find("input[name=unit_type_name]").val(unit_type_name).attr('readonly','readonly');
			dialogLayer.find("input[name=unit_super_id]").val(id).attr('readonly','readonly');
			dialogLayer.find("input[name=unit_super_name]").val(super_name).attr('readonly','readonly');
			dialogLayer.find("input[name=oper]").val(oper);
			
			//dialogLayer.find("select[name=unit_type]").parent().css('display',css)
			//dialogLayer.find("input[name=unit_super_id]").parent().css('display',css).attr('readonly','readonly');
			//dialogLayer.find("input[name=unit_super_name]").parent().css('display',css).attr('readonly','readonly');
			
			if(only){
			dialogLayer.find("input[name=unit_name]").attr('readonly','readonly');
			}else{
			dialogLayer.find("input[name=unit_name]").removeAttr('readonly');	
			}
			//'readonly','readonly' '删除单位'
			dialogLayer.dialog({'title':title});
			dialogLayer.dialog("open");	
			
		},
		
		save : function(){
			var dialogLayer = $(this.options.dialogId);;
				
			dialogLayer.find('input[name=commit]').click(function(){
				var id = dialogLayer.find("input[name=unit_super_id]").val(),
					type = dialogLayer.find("input[name=unit_type]").val(),
					name = dialogLayer.find("input[name=unit_name]").val(),
					oper = dialogLayer.find("input[name=oper]").val();
					
				$.ajax({
					url : 'index.php?r=unit/Op',
					data : {
						'id' : id,
						'type' : type,
						'name' : name,
						'op' : oper	
					},
					type : 'post',
					success : function(data){						
						if(data['no'] == 0){
							dialogLayer.dialog("close");
							window.location.href = window.location.href;
						}else{
							if(typeof(data['error']) != 'string'){
								for(var i in data['error']){
									alert(data['error'][i]);	
								}	
							}else{
								alert(data['error']);
							}
						}
					},
					dataType : 'json'
				});
			});				
		},
		
		toggle : function(){
			$('.collapse').bind('click',function(){
				var _class = '',
					layer = $('ul[data-super-id='+$(this).attr('id')+']');
				
				_class = $(this).hasClass('collapsed')?'collapse':'collapsed';
				$(this).attr('class',_class);
				layer.toggle('collapsed');
			});
		},
		
		init : function(){
			this.bind();
			this.save();
			this.toggle();
		}		
	};	
	
	Unit.init();
	
	$.Unit = Unit;
})(jQuery);